import os, array, zlib
import re
import path
import cPickle


file_crc_dic = {}
history_file = "history.ksm"

dirs_to_monitor = ["blogger_dot_com"]

def GetCRC32(filename):
    fileSize = os.stat(filename)[6]
    f = open(filename, "rb")
    v = array.array('B')
    try:
        v.fromfile(f, fileSize)
        i = zlib.crc32(v.tostring())
        return i
    except:
        return None
   
def Update()
    try:
        f_history = open(history_file)
        version = cPickle.load(f_history)
        if version == 1.0:
            file_crc_dic = cPickle.load(f_history)
            fo.close()
        else:
            print "Version error..."
            sys.exit(1)
    except:
        pass
    
    for dir_to_monitor in dirs_to_monitor:
        path_to_monitor = path.path(".\\%s"%dir_to_monitor)
        for file in path_to_monitor.files():
            if file_crc_dic.has_key(file):
                if GetCRC32(file) != file_crc_dic[file]:
                    print "%s has changed" % file
            else:
                print "%s has created" % file
                file_crc_dic[file] = GetCRC32(file)

    fo = open( history_file, "wb")
    version = 1.0
    cPickle.dump( version, fo)
    cPickle.dump( file_crc_dic, fo )
    fo.close()
                
"""
import win32file
import win32con

ACTIONS = {
  1 : "Created",
  2 : "Deleted",
  3 : "Updated",
  4 : "Renamed from something",
  5 : "Renamed to something"
}
# Thanks to Claudio Grondi for the correct set of numbers
FILE_LIST_DIRECTORY = 0x0001

if __name__ == "__main__":
    path_to_watch = "."
    hDir = win32file.CreateFile (
            path_to_watch,
            FILE_LIST_DIRECTORY,
            win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
            None,
            win32con.OPEN_EXISTING,
            win32con.FILE_FLAG_BACKUP_SEMANTICS,
            None
            )
    count = 1

    while 1:
        #
        # ReadDirectoryChangesW takes a previously-created
        #  handle to a directory, a buffer size for results,
        #  a flag to indicate whether to watch subtrees and
        #  a filter of what changes to notify.
        #
        # NB Tim Juchcinski reports that he needed to up
        #  the buffer size to be sure of picking up all
        #  events when a large number of files were
        #  deleted at once.# 
        results = win32file.ReadDirectoryChangesW (
                hDir,
              1024,
                True,
                win32con.FILE_NOTIFY_CHANGE_FILE_NAME | 
                win32con.FILE_NOTIFY_CHANGE_DIR_NAME |
                win32con.FILE_NOTIFY_CHANGE_ATTRIBUTES |
                win32con.FILE_NOTIFY_CHANGE_SIZE |
                win32con.FILE_NOTIFY_CHANGE_LAST_WRITE |
                win32con.FILE_NOTIFY_CHANGE_SECURITY,
                None,
                None
              )
        for action, file in results:
            full_filename = os.path.join (path_to_watch, file)
            dirname = os.path.dirname(full_filename)
          
            if file.endswith("swp"):
                continue
            if file.endswith("~"):
                continue
            if os.path.isdir(full_filename):
                continue
            if re.match(r"^[0-9]+$", file):
                continue

            for dir_to_monitor in dirs_to_monitor:
                if dirname.upper().find(dir_to_monitor.upper()):
                    update()"""

